package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.LibListBean;
import cn.bocweb.spinning.model.Bean.MatchInviteBean;
import cn.bocweb.spinning.model.Bean.MyClassBean;
import cn.bocweb.spinning.model.Bean.MyClassNewBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.util.WXUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.MagnificentChart;
import cn.bocweb.spinning.view.common.MagnificentChartItem;
import de.greenrobot.event.EventBus;

public class CountActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.magnificentChart)
    MagnificentChart magnificentChart;

    MyClassNewBean.RecordBean progressEntity;
    @Bind(R.id.count_detail_wrong)
    TextView countDetailWrong;
    @Bind(R.id.count_detail_wrong_percent)
    TextView countDetailWrongPercent;
    @Bind(R.id.count_detail_right)
    TextView countDetailRight;
    @Bind(R.id.count_detail_right_percent)
    TextView countDetailRightPercent;
    @Bind(R.id.count_detail_miss)
    TextView countDetailMiss;
    @Bind(R.id.count_detail_miss_percent)
    TextView countDetailMissPercent;

    boolean isMianToCount = false;
    UserPresenter presenter;
    @Bind(R.id.share_wechat_friend)
    LinearLayout mShareWechatFriend;
    @Bind(R.id.share_wechat_timeline)
    LinearLayout mShareWechatTimeline;

    int friend_1_timeline_2 = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initToolbar();
        initIntent();
        loadData();

        mShareWechatFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.matchInvite();
                friend_1_timeline_2 = 1;
            }
        });

        mShareWechatTimeline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.matchInvite();
                friend_1_timeline_2 = 2;
            }
        });
    }

    public void onEventMainThread(MatchInviteBean bean) {
        if (bean.getReturn_code() == 0) {
            if (friend_1_timeline_2 == 1) {
                WXUtil.WXShareToFriend(bean.getUrl(), this);
            } else {
                WXUtil.WXShareToTimeline(bean.getUrl(), this);
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initIntent() {
        Intent intent = getIntent();
        if (intent.hasExtra(Constant.COUNT_ACITIVYT)) {
            progressEntity = (MyClassNewBean.RecordBean) intent.getSerializableExtra(Constant.COUNT_ACITIVYT);
        } else {
            isMianToCount = intent.getBooleanExtra(Constant.MAIN_TO_COUNT, Constant.IS_MAIN_TO_COUNT);
        }
    }

    private void initToolbar() {
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setTitle("统计");
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void loadData() {
        if (isMianToCount) {
            UserBean.DataEntity user = new UserBean.DataEntity();
            user.readInfoFromLocal(this);
            LibListBean.DataEntity lib = new LibListBean.DataEntity();
            lib.readLibListIntoLocal(this);
            presenter = new UserPresenterImp();
            presenter.myClass(user.getUserid(), lib.getId());
            showLoading();
        } else {
            presenter = new UserPresenterImp();

            DecimalFormat df0 = new DecimalFormat("######0");
            DecimalFormat df1 = new DecimalFormat("######0.0");
            DecimalFormat df2 = new DecimalFormat("######0.00");

            double total = progressEntity.getTotal();
            double missCount = progressEntity.getBlank();
            double rightCount = progressEntity.getRight();
            double wrongCount = progressEntity.getWrong();

            double wrong = progressEntity.getWrong() * 100 / total;
            double right = progressEntity.getRight() * 100 / total;
            double miss = 100 - wrong - right;

            magnificentChart.setText(df0.format(total - missCount));
            magnificentChart.setChartBackgroundColor(getResources().getColor(R.color.line));

            MagnificentChartItem item = new MagnificentChartItem("做对", right, getResources().getColor(R.color.green));
            MagnificentChartItem item1 = new MagnificentChartItem("做错", wrong, getResources().getColor(R.color.red));
//            MagnificentChartItem item2 = new MagnificentChartItem("未做", miss / total * 100, getResources().getColor(R.color.yellow));
            MagnificentChartItem item3 = new MagnificentChartItem("未做", miss, getResources().getColor(R.color.colorPrimary));

            List<MagnificentChartItem> list = new ArrayList<>();
            list.add(item);
            list.add(item1);
//            list.add(item2);
            list.add(item3);

            magnificentChart.setChartItemsList(list);
            magnificentChart.setMaxValue(100);

            countDetailRight.setText(df0.format(rightCount));
            countDetailWrong.setText(df0.format(wrongCount));
            countDetailMiss.setText(df0.format(missCount));

            float rightShow = (float) (Math.round(Double.parseDouble(df2.format(right * 10)))) / 10;
            float wrongShow = (float) (Math.round(Double.parseDouble(df2.format(wrong * 10)))) / 10;
            float missShow = 100 - rightShow - wrongShow;

            countDetailRightPercent.setText(String.valueOf(df1.format(rightShow)) + "%");
            countDetailWrongPercent.setText(String.valueOf(df1.format(wrongShow)) + "%");
            countDetailMissPercent.setText(String.valueOf(df1.format(missShow)) + "%");

//            shareWechatFriend.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    WXUtil.WXShareToFriend("")
//                }
//            });
//
//            shareWechatTimeline.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                }
//            });
        }
    }

    public void onEventMainThread(MyClassBean bean) {


        DecimalFormat df0 = new DecimalFormat("######0");
        DecimalFormat df1 = new DecimalFormat("######0.0");
        DecimalFormat df2 = new DecimalFormat("######0.00");

        MyClassBean.ProgressEntity progressEntity = bean.getProgress();
        double total = progressEntity.getTotal();
        double missCount = progressEntity.getBlank();
        double wrongCount = progressEntity.getWrong();
        double rightCount = progressEntity.getRight();

        double wrong = progressEntity.getWrong() * 100 / total;
        double right = progressEntity.getRight() * 100 / total;
        double miss = 100 - wrong - right;

        magnificentChart.setText(df0.format(total - missCount));

        MagnificentChartItem item = new MagnificentChartItem("做对", right, getResources().getColor(R.color.green));
        MagnificentChartItem item1 = new MagnificentChartItem("做错", wrong, getResources().getColor(R.color.red));
//        MagnificentChartItem item2 = new MagnificentChartItem("未做", , getResources().getColor(R.color.yellow));
        MagnificentChartItem item3 = new MagnificentChartItem("未做", miss, getResources().getColor(R.color.colorPrimary));

        List<MagnificentChartItem> list = new ArrayList<>();
        list.add(item);
        list.add(item1);
        list.add(item3);


        magnificentChart.setChartItemsList(list);
        magnificentChart.setMaxValue(100);


        countDetailRight.setText(df0.format(rightCount));
        countDetailWrong.setText(df0.format(wrongCount));
        countDetailMiss.setText(df0.format(missCount));

        float rightShow = (float) (Math.round(Double.parseDouble(df2.format(right * 10)))) / 10;
        float wrongShow = (float) (Math.round(Double.parseDouble(df2.format(wrong * 10)))) / 10;
        float missShow = 100 - rightShow - wrongShow;

        countDetailRightPercent.setText(String.valueOf(df1.format(rightShow)) + "%");
        countDetailWrongPercent.setText(String.valueOf(df1.format(wrongShow)) + "%");
        countDetailMissPercent.setText(String.valueOf(df1.format(missShow)) + "%");

        hideLoading();
    }

    public void onEventMainThread(ErrorBean bean) {
        DeviceUtil.MyToast(this, bean.getErrorMsg());
    }
}